package com.jason.mvptest3.presenter;

import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;

import com.jason.mvptest3.bean.Person;
import com.jason.mvptest3.itfcontract.LoginContract;

/**
 * @Author: Jason
 * @Date: 2016/11/24
 * @Time: 14:46.
 */

public class LoginPresenter implements LoginContract.Presenter {

    private LoginContract.View mView;
    private Handler mHandler;

    public LoginPresenter(LoginContract.View view) {
        this.mView = view;
        this.mHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    public void loadingTask() {

        final String username = mView.getUserName();
        final String password = mView.getPassWord();

        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
            mView.showLoadingDialog();
            new Thread(new Runnable() {
                @Override
                public void run() {

                    SystemClock.sleep(3000);

                    if ("jason".equals(username) && "123456".equals(password)) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                mView.hideLoadingDialog();
                                mView.toActivity(new Person("jason", "123456"));
                            }
                        });
                    } else {
                        // 用户名和密码不对
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                mView.hideLoadingDialog();
                                mView.showLoadingError("用户名或者密码不正确");
                            }
                        });
                    }

                }
            }).start();

        } else {
            mView.showUserNameOrPassWordEmpty();
        }

    }

    @Override
    public void result(int requestCode, int resultCode) {

    }

    @Override
    public void start() {

    }
}
